"""
生成离散信源
"""
import math
import numpy as np
import random


class Info:
    def __init__(self):
        self.createList()

    def createList(self):
        """
        :return:
        """
        self.source()
        self.H_SingleWord()
        self.Redundancy()
    def source(self):
        """
        生成随机序列 7-10 ，随机产生概率
        :return:
        """
        length = random.randint(7, 10)
        sourceList = []
        total = 1
        for i in range(0, length - 1):
            randi = random.uniform(0, total)
            sourceList.append(randi)
            total -= randi
        sourceList.append(total)
        self.sourceList = sourceList

    def I(self, message):
        return -(math.log(message) / math.log(2))
    def H_SingleWord(self):
        if len(self.sourceList) != 0:
            sum = 0
            for i in self.sourceList:
                sum += i * self.I(i)
            self.H = sum
        else:
            self.H = 0

    def Redundancy(self):
        Hmax = self.I(1 / len(self.sourceList))
        self.redundancy = 1 - self.H / Hmax
    def printList(self):
        for i in self.sourceList:
            print(str(i))
        print("H is :")
        print(str(self.H))
        print("Redundancy is :")
        print(str(self.redundancy))


#
# def WordLength(input, wordLenth):
#     if len(wordLenth) != 0:
#         aveLenth = 0
#         for i in range(0, len(wordLenth)):
#             aveLenth += wordLenth[i] * input[i]
#         return aveLenth
#     else:
#         return 0
#
#
# def H_MultiWord(input, wordLenth):
#     if len(input) != 0:
#         sum = 0
#         for i in input:
#             sum += i * I(i)
#         return [sum, WordLength(input, wordLenth)]
#     else:
#         return 0


